package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.dao.BussinessDao;
import com.baichang.taoshunda.dao.CompanyPanymentHistoryDao;
import com.baichang.taoshunda.dao.RecommendCompanyDao;
import com.baichang.taoshunda.entity.Bussiness;
import com.baichang.taoshunda.entity.CompanyPanymentHistory;
import com.baichang.taoshunda.entity.RecommendCompany;
import com.baichang.taoshunda.entity.em.currencyCode;
import com.baichang.taoshunda.entity.em.payHistory;
import com.baichang.taoshunda.service.RecommendCompanyService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/29 0029.
 */
@Service
public class RecommendCompanyServiceImpl implements RecommendCompanyService {

    @Autowired
    private BussinessDao bussinessDao;

    @Autowired
    private CompanyPanymentHistoryDao companyPanymentHistoryDao;

    @Autowired
    private RecommendCompanyDao recommendCompanyDao;

    /**
     * 添加推荐商家
     *
     * @param busId
     * @param month
     * @param price
     * @return
     */
    @Override
    public boolean addRecommendBus(Integer busId, Integer month, BigDecimal price) {
//        Bussiness business = bussinessDao.getEntityById(busId);
//        //轮播图的交易记录
//        CompanyPanymentHistory companyPanymentHistory = new CompanyPanymentHistory();
//        companyPanymentHistory.setCompanyId(busId);
//        companyPanymentHistory.setPaymentMoney(price);
//        companyPanymentHistory.setPaymentTime(month);
//        companyPanymentHistory.setPaymentType(payHistory.诚信商家.getCode());
//        companyPanymentHistory.setCreatedTime(new Date());
//        companyPanymentHistoryDao.save(companyPanymentHistory);
//        //查询最新创建的数据id
//        Map<String, Object> map = companyPanymentHistoryDao.queryHistoryId();
//        String historyId = map.get("id").toString();
//        int payId = Integer.parseInt(historyId);
//        RecommendCompany recommendCompany = new RecommendCompany();
//        recommendCompany.setCompanyId(busId);
//        recommendCompany.setStatus(1);
//        recommendCompany.setAuditingStatus(currencyCode.申核中.getCode());
//        recommendCompany.setNumber(0);
//        return recommendCompanyDao.save(recommendCompany) > 0;
        return true;
    }


    /**
     * 查询我的推荐商家
     *
     * @param busId
     * @return
     */
    @Override
    public Map<String, Object> queryRecommendBusDetails(Integer busId) throws ServiceException, ParseException {
        Map<String, Object> map = new HashedMap();
        //查询这个商家有没有申请推荐诚信商家
        List<Map<String, Object>> mapTime = recommendCompanyDao.queryCompanyId(busId);
        if (mapTime == null) {
            map.put("judge", "false");
            return map;
        }else {
            for (int i = 0; i < mapTime.size(); i++) {
                String id = mapTime.get(i).get("id").toString();
                //先判断时间是不是为空
                String time = mapTime.get(i).get("endTime").toString();
                if (!time.equals("0")) {
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                    Date endTime = df.parse(time); //结束时间
                    //当前时间
                    String createTime = df.format(new Date());
                    Date data = df.parse(createTime);
                    int a = endTime.compareTo(data);  //结束时间在前  结束时间小于当前时间  -1   结束时间大于当前时间为1   等于为0
                    if (a == -1) {
                        //结束时间小于当前时间 修改状态为0
                        recommendCompanyDao.updateRecommendStaus(Integer.valueOf(id),0);
                        map.put("judge", "false");
                    } else {
                        //返回的是展示中并且在结束时间内的
                        map = recommendCompanyDao.queryRecommendDetails(Integer.valueOf(id));
                        map.put("judge", "true");
                    }
                } else {
                    //返回的是待审核和待支付的
                    map = recommendCompanyDao.queryRecommendDetails(Integer.valueOf(id));
                    map.put("judge", "true");
                }
            }
        }
        return map;
    }
}
